<?php
header("Access-Control-Allow-Origin: *"); // 允许所有源，或者替换为特定的源
header("Access-Control-Allow-Methods: POST, OPTIONS , GET"); // 允许POST和OPTIONS请求
header("Access-Control-Allow-Headers: Content-Type"); // 允许Content-Type头

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit;
}

$tagsclick=<<<TEXT
javascript:(function () {
    const url = window.location.href;
    let content = document.documentElement.outerHTML;
    fetch('https://shop.shante.me/yunshop/au3/save.php?type=getJsScript&XDEBUG_SESSION_START=5432534', {
        method: 'POST',
        headers: {'Content-Type': 'application/json',},
        body: JSON.stringify({url: url, content: content}),
    }).then(response => response.json()).then(data => {
        if(data.jslink!=null){
            if(top.document.querySelector("#shantejs")){
                top.document.querySelector("#shantejs").remove();
            }
            var scriptElement = document.createElement('script');
            scriptElement.id="shantejs";
            scriptElement.src  = data.jslink;
            document.head.appendChild(scriptElement);
        }
        if(top.document.querySelector("#scriptid")){
            top.document.querySelector("#scriptid").remove();
        }
        var scriptElement = document.createElement('script');
        scriptElement.id="scriptid";
        scriptElement.textContent  = data.js;
        document.head.appendChild(scriptElement);
        window.onload=function(){
            eval(data.script);
        }
    }).catch(error => console.error('Error:', error));
})();
TEXT;
if($_REQUEST['type']=="getJsScript") {
    $raw_post_data = file_get_contents('php://input');
    $data_post = json_decode($raw_post_data, true);
    $url=$data_post["url"];
    $item_id=512;
    if(preg_match("/item_id=([0-9]+)/",$url,$nn)||$item_id){
        if(!$item_id) $item_id=$nn[1];
        $url=preg_replace("/(^http[^\?\s\"']+?).*$/Uisx","\\1",$url);

        $jsContent= file_get_contents("public.js");
        if(preg_match("/shopee\.cn\/portal\/product\/mtsku\/new/i",$url)){
            $jsContent.=file_get_contents("save_shopee.js");
        }
        if(preg_match("/weixin\.qq\.com\/shop\/goods\/entry/i",$url)){
            $jsContent.=file_get_contents("save_weixin.js");
        }
        if(preg_match("/jinritemai\.com\/ffa\/g\/create/i",$url)){
            $jsContent.=file_get_contents("save_douyin.js");
        }
        $data=array();
        $data['code']=1;

        $jsContent = preg_replace_callback("/\{\\$([^\s]+)\}/Uisx", function ($em) use ($url, $data,$item_id) {
            return eval("return trim($".$em[1].");");
        }, $jsContent);
        $jsfilename="save.js";
        file_put_contents($_SERVER['DOCUMENT_ROOT']."/public/temp/".$jsfilename,$jsContent);
        $data["jslink"]="https://shop.shante.me/Public/temp/".$jsfilename."?version=".date("YmdH");  //;."?version=".date("YmdH")
        $data['js']="if(typeof save === 'function') save();";
    }else{
        $data=array();
        $data['code']=0;
        $data['message']="出错了，请抓取闪特云仓的网址！";
    }
    echo json_encode($data);
    exit;
}

if($_REQUEST['type']=="script"){
    $data=array();
    $script=<<<TEXT2


TEXT2;
    $data["script"]=$script;
    echo json_encode($data);
}





